package Lee_code;

/**
 * @author blh
 * @date 2024年10月20日 9:43
 */
public class Lee_6_1020 {
    public String convert(String s, int numRows) {
        int len = s.length();
        char[][] matrix = new char[numRows][len / 2 + 1];
        int index = 0, row = 0, column = 0;
        if (numRows == 1) {
            return s;
        }
        while (index < len) {
            // 从上到下
            for (row = 0; (row < numRows) && (index < len); row++) {
                matrix[row][column] = s.charAt(index++);
            }
            // 左下到右上
            for (row = numRows - 2; (row > 0) && (index < len); row--) {
                matrix[row][++column] = s.charAt(index++);
            }
            column++;
        }
        return convert(matrix);
    }

    private String convert(char[][] matrix) {
        StringBuilder sb = new StringBuilder();
        for (char[] chars : matrix) {
            for (char aChar : chars) {
                if (aChar != 0) {
                    sb.append(aChar);
                }
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Lee_6_1020 method = new Lee_6_1020();
        System.out.println(method.convert("PAYPALISHIRING", 3));
    }
}
